package mobile

import (
	"net/http"

	"gitee.com/cxssang/goframe/lib/utils"

	"gitee.com/cxssang/goframe/lib/sql"

	. "gitee.com/cxssang/genealogy/act"
)

func init() {
	HandleFunMap["/m/tree"] = func(w http.ResponseWriter, r *http.Request) {
		sq := sql.New()
		rows, err := sq.Select("members", sql.NewWhere(), 0, 0, "id,name,spouse,sex,birth,parent_id,sort", "sort asc").FetchAll()
		if err != nil {
			DisplayError(w, err.Error())
			return
		}
		if rows == nil {
			rows = sql.Rows{}
		}

		var nodes []*Node
		for _, row := range rows {
			node := &Node{
				ID:       row.GetInt("id"),
				Name:     row.Get("name"),
				ParentID: row.GetInt("parent_id"),
				Spouse:   row.Get("spouse"),
				Sex:      row.Get("sex"),
				Birth:    row.Get("birth"),
			}
			nodes = append(nodes, node)
		}
		tree := BuildTree(nodes, 0)
		DisplayM(w, r, "mobile/tree", map[string]interface{}{
			"tree":  CreateTree(tree),
			"title": "关系图 - 族谱管理",
		})
	}

	HandleFunMap["/m/tree/add"] = func(w http.ResponseWriter, r *http.Request) {
		name := r.PostFormValue("name")
		sex := r.PostFormValue("sex")
		birth := r.PostFormValue("birth")
		if CheckEmpty(w, name, "请输入姓名") {
			return
		}
		if CheckEmpty(w, sex, "请选择性别") {
			return
		}
		sq := sql.New()
		parentId := r.Header.Get("member-id")
		where := sql.NewWhere().Add("name=?", name).Add("parent_id=?", parentId)
		count, _ := sq.Count("members", where, "id")
		if count > 0 {
			JsonError(w, "该姓名已存在")
			return
		}
		ret, err := sq.Insert("members", map[string]interface{}{
			"name":      name,
			"sex":       sex,
			"birth":     birth,
			"parent_id": parentId,
		}).Exec()
		if err != nil {
			JsonError(w, err.Error())
			return
		}
		mid, _ := ret.LastInsertId()
		_, err = sq.Insert("users", map[string]interface{}{
			"name":      name,
			"password":  utils.Md5("123456"),
			"member_id": mid,
			"is_admin":  "0",
		}).Exec()
		if err != nil {
			JsonError(w, err.Error())
			return
		}
		_, err = sq.Update("members", sql.NewWhere().Add("id=?", mid), map[string]interface{}{
			"sort": mid,
		}).Exec()
		if err != nil {
			JsonError(w, err.Error())
			return
		}
		JsonSuccess(w, nil)

	}

	HandleFunMap["/m/tree/edit"] = func(w http.ResponseWriter, r *http.Request) {
		id := r.PostFormValue("id")
		name := r.PostFormValue("name")
		sex := r.PostFormValue("sex")
		birth := r.PostFormValue("birth")
		sort := r.PostFormValue("sort")
		if CheckEmpty(w, id, "缺少ID") {
			return
		}
		if CheckEmpty(w, name, "请输入姓名") {
			return
		}
		if CheckEmpty(w, sex, "请选择性别") {
			return
		}
		if sort == "" {
			sort = "99"
		}
		sq := sql.New()
		parentId := r.Header.Get("member-id")
		where := sql.NewWhere().Add("id!=?", id).Add("name=?", name).Add("parent_id=?", parentId)
		count, _ := sq.Count("members", where, "id")
		if count > 0 {
			JsonError(w, "该姓名已存在")
			return
		}
		_, err := sq.Update("members", sql.NewWhere().Add("id=?", id), map[string]interface{}{
			"name":  name,
			"sex":   sex,
			"birth": birth,
			"sort":  sort,
		}).Exec()
		if err != nil {
			JsonError(w, err.Error())
		} else {
			JsonSuccess(w, nil)
		}
	}

	HandleFunMap["/m/tree/del"] = func(w http.ResponseWriter, r *http.Request) {
		id := r.PostFormValue("id")
		if CheckEmpty(w, id, "缺少ID") {
			return
		}
		sq := sql.New()
		count, err := sq.Count("members", sql.NewWhere().Add("parent_id=?", id), "id")
		if err != nil {
			JsonError(w, err.Error())
			return
		}
		if count > 0 {
			JsonError(w, "该成员下有子成员，不能删除")
			return
		}
		_, err = sq.Delete("members", sql.NewWhere().Add("id=?", id)).Exec()
		if err != nil {
			JsonError(w, err.Error())
			return
		}
		_, err = sq.Delete("users", sql.NewWhere().Add("member_id=?", id)).Exec()
		if err != nil {
			JsonError(w, err.Error())
			return
		}

		JsonSuccess(w, nil)
	}
}
